import 'package:dio/dio.dart';
import 'package:flutter_wan_android/core/http/network_module.dart';
import 'package:flutter_wan_android/data/base_data_layer_api.dart';
import 'package:flutter_wan_android/data/user/user_entity.dart';

import '../wan_android_resp.dart';


/// 网络数据api
class UserNetworkApi {
  late final Dio _dio;

  UserNetworkApi(NetworkModule module) {
    _dio = module.dio;
  }

  /// 登录
  Future<UserEntity> login(String account, String password) async{
    return await performNetworkRequest(
      () => _dio.post(
        "user/login",
        data: FormData.fromMap({"username": account, "password": password}),
      ),
      (body) => WanAndroidResp.fromJson(body, (data) => UserEntity.fromJson(data as Map<String,dynamic>)),
    );
  }

  /// 注册
  Future<void> register(String account, String password, String rePassword) {
    return performNetworkRequest(
      () => _dio.post(
        "user/register",
        data: FormData.fromMap({
          "username": account,
          "password": password,
          "repassword": rePassword,
        }),
      ),
      (body) => WanAndroidResp.fromJson(body, (data) => data),
    );
  }
}
